//
//  SettingViewController.m
//  dogGroup
//
//  Created by iOS Two on 14/12/5.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "SettingViewController.h"
#import "SDImageCache.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatController];
    if (version >= 7) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        titleLabel.text = @"设置";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    } else {
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        self.navigationItem.title = @"设置";
    }
    // Do any additional setup after loading the view.
}
-(void)creatController {
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, 568)];
    image.image = [UIImage imageNamed:@"dididid_01"];
    [self.view addSubview:image];
    
    _back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
    _back.backgroundColor = [UIColor clearColor];
    [_back addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnkey)]];
    [self.view addSubview:_back];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 21, kwidth, 101.5)];
    [button setBackgroundImage:[UIImage imageNamed:@"heihei_03"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 1;
    [_back addSubview:button];
    
    UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 151, kwidth, 227.5)];
    image2.image = [UIImage imageNamed:@"heihei_07"];
    [_back addSubview:image2];
    
    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(83.5, 332, 152, 32.5)];
    [button1 setBackgroundImage:[UIImage imageNamed:@"tijiao_07"] forState:UIControlStateNormal];
    button1.tag = 2;
    [button1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_back addSubview:button1];
    
    _text = [[UITextView alloc]initWithFrame:CGRectMake(20, 185, 282, 130)];
    _text.backgroundColor = [UIColor clearColor];
    _text.returnKeyType = UIReturnKeyDone;
    _text.delegate = self;
    [_back addSubview:_text];
}
-(void)returnkey {
    [_text resignFirstResponder];
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
    _back.frame = CGRectMake(0, -150, kwidth, kHeight);
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    _back.frame = CGRectMake(0, 0, kwidth, kHeight);
}

-(void)buttonClick:(UIButton *)sender {
    if (sender.tag == 1) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"是否清除缓存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    } else {
        if ([_text.text isEqualToString:@""]) {
            [self alertViewShow:@"内容不能为空,请重新输入"];
        } else {
            AVObject *object = [AVObject objectWithClassName:@"feedback"];
            [object setObject:_text.text forKey:@"feedBack"];
            [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    [self alertViewShow:@"感谢您的反馈"];
                } else {
                    [self alertViewShow:@"可能出错了,请重试下"];
                }
            }];
        }
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [AVQuery clearAllCachedResults];
        [[SDImageCache sharedImageCache] clearDisk];
        [[SDImageCache sharedImageCache] clearMemory];
        [self alertViewShow:@"清除成功"];
    } else {
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
